package com.platform.hjt.erp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.StateValue;
import com.platform.hjt.base.exception.BizException;
import com.platform.hjt.erp.model.ErpStorageFlow;
import com.platform.hjt.erp.service.ErpStorageFlowDetailService;
import com.platform.hjt.erp.service.ErpStorageFlowService;
import com.platform.hjt.from.model.FromQjOrder;
import com.platform.hjt.from.service.FromQjOrderService;

@Controller
@RequestMapping(value="/erpStorageFlow")
public class ErpStorageFlowController extends BaseController{

	@Resource
	private ErpStorageFlowService erpStorageFlowService;
	@Resource
	private ErpStorageFlowDetailService erpStorageFlowDetailService;
	@Resource
	private FromQjOrderService fromQjOrderService;
	 
	@RequestMapping(value="/erpStorageFlow_list.html")
	public String erpStorageFlowListPage(){
		return "page/admin/erpStorageFlow/erpStorageFlow_list";
	}
	
	@RequestMapping(value="/erpStorageFlow_list.do")
	@ResponseBody
	public Page<ErpStorageFlow> erpStorageFlowListDo(Page<ErpStorageFlow>page){
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		return erpStorageFlowService.getErpStorageFlowList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/erpStorageFlow_edit.html")
	public String erpStorageFlowEditPage(HttpServletRequest request, Model model, String id){
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("erpStorageFlow", erpStorageFlowService.getErpStorageFlow("storageFlowId", id));
		}
		return "page/admin/erpStorageFlow/erpStorageFlow_edit";
	}
	
	@RequestMapping(value="/erpStorageFlow_out.html")
	public String erpStorageFlowOutPage(HttpServletRequest request, Model model, String id){
		return "page/admin/erpStorageFlow/erpStorageFlow_out";
	}
	
	@RequestMapping(value="/erpStorageFlow_detail_{id}.html")
	public String erpStorageFlowDetailPage(Model model, @PathVariable String id){
		
		model.addAttribute("erpStorageFlow", erpStorageFlowService.getErpStorageFlow("storageFlowId", id));
		
		List<PropertyFilter>  propertyFilterList = new ArrayList<PropertyFilter>();
		propertyFilterList.add(new PropertyFilter("EQS_erpStorageFlow.storageFlowId", id));
		model.addAttribute("erpStorageFlowDetailList", erpStorageFlowDetailService.getErpStorageFlowDetailList(propertyFilterList));
		
		return "page/admin/erpStorageFlow/erpStorageFlow_detail";
	}
	
	@RequestMapping(value="/erpStorageFlow_edit.do")
	@ResponseBody
	public ReturnBody erpStorageFlowEditDo(@Valid @ModelAttribute("erpStorageFlow") ErpStorageFlow erpStorageFlow, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		ReturnBody rb;
		try{
			rb = erpStorageFlowService.saveErpStorageFlow(erpStorageFlow);
		}catch(BizException e){
			rb = new ReturnBody(false, e.getMessage());
		}
		
		return rb;
	}
	
	@RequestMapping(value="/erpStorageFlow_del_{id}.do")
	@ResponseBody
	public ReturnBody erpStorageFlowDelDo(@PathVariable String id){
		return erpStorageFlowService.delErpStorageFlow(new ErpStorageFlow(id));
	}
	
	
	
	
	/**
	 * 关联订单出入库
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/erpStorageFlowP_edit.html")
	public String erpStorageFlowPEditPage(HttpServletRequest request, Model model, String id, String processId, String orderId, String taskId, String taskName, boolean readonly){
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		if(StringUtils.isNotBlank(orderId)){
			ErpStorageFlow erpStorageFlow = erpStorageFlowService.getErpStorageFlow("processOrderId", orderId);
			model.addAttribute("erpStorageFlow", erpStorageFlow);
		}
		
		FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
		model.addAttribute("fromQjOrder", fromQjOrder);
		
		Map<String, Object>map = new HashMap<String, Object>();
		map.put("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId());
		map.put("isProduce", true);
		
		model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList(map));
		
		//if(StringUtils.isNotBlank(orderId) && StringUtils.isNotBlank(taskId)){
		return "page/admin/erpStorageFlow/erpStorageFlowP_edit";
	}
	
	@RequestMapping(value="/erpStorageFlowP_edit.do")
	@ResponseBody
	public ReturnBody erpStorageFlowPEditDo(@Valid @ModelAttribute("erpStorageFlow") ErpStorageFlow erpStorageFlow, BindingResult result, String processId, String processOrderId, String taskId){
		ReturnBody rb;
		try{
			rb = erpStorageFlowService.saveErpStorageFlowP(erpStorageFlow, processId, processOrderId, taskId);
		}catch(BizException e){
			rb = new ReturnBody(false, e.getMessage());
		}
		
		return rb;
	}
	
	
	@RequestMapping(value="/erpStorageFlowP_out.html")
	public String erpStorageFlowPOutPage(HttpServletRequest request, Model model, String id, String processId, String orderId, String taskId, String taskName, boolean readonly){
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
		model.addAttribute("fromQjOrder", fromQjOrder);
		
		Map<String, Object>map = new HashMap<String, Object>();
		map.put("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId());
		map.put("isProduce", true);
		model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList(map));
		
		Map<String, Object> storageMap = new HashMap<String, Object>();
		storageMap.put("processId", processId);
		storageMap.put("processOrderId", orderId);
		storageMap.put("storageFlowType", StateValue.InStorage.getKey());
		ErpStorageFlow erpStorageFlow = erpStorageFlowService.getErpStorageFlow(storageMap);
		model.addAttribute("erpStorageFlow", erpStorageFlow);
		
		Map<String, Object>detailMap = new HashMap<String, Object>();
		detailMap.put("erpStorageFlow.storageFlowId", erpStorageFlow.getStorageFlowId());
		model.addAttribute("erpStorageFlowList", erpStorageFlowDetailService.getErpStorageFlowDetailList(detailMap));
		
		String hql = "select t from ErpStorageFlowDetail t, ErpStorageFlow e where t.erpStorageFlow.storageFlowId = e.storageFlowId and e.processId = :processId and e.processOrderId=:processOrderId and e.storageFlowType=:storageFlowType";
		Map<String, Object>values = new HashMap<String, Object>();
		values.put("processId", processId);
		values.put("processOrderId", orderId);
		values.put("storageFlowType", StateValue.OutStorage.getKey());
		model.addAttribute("outList", erpStorageFlowDetailService.getErpStorageFlowDetailList(hql, values));
		
		return "page/admin/erpStorageFlow/erpStorageFlowP_out";
	}
	
	
}
